#ifndef SEARCHEROPTION_H
#define SEARCHEROPTION_H

#include <QThread>
#include <QMutex>
#include <QDir>
#include <QReadWriteLock>

#include "messagedeliver.h"
#include "parallelsearcher.h"

class QThreadPool;
class MessageDeliver;
class QSemaphore;
class QStandardItemModel;
class QStandardItem;

class OperationHandler : public QThread
{
	Q_OBJECT

enum SearcherOption{
	DoSearch,
	DoSearchAndReplace,
	DoExportToXML,
	DoPrintResult
};

public:
	OperationHandler(QObject *object, MessageDeliver *msgDeliver);
	~OperationHandler();

	bool setSearchParameter(
		const QDir &searchDir, 
		const QString &dirFilter, 
		const QString &pattern, 
		bool isCaseSensitive, 
		const QString &encoding = "ANSI" /* Useless so far, remain for manual change encode */);

	bool setReplaceParameter(
		const QDir &searchDir, 
		const QString &dirFilter, 
		const QString &pattern, 
		bool isCaseSensitive, 
		const QString &replaceContent, 
		const QString &encoding = "ANSI" /* Useless so far, remain for manual change encode */);

	bool setExportXMLParameter(const QString &path, const QStandardItemModel *model);
	bool setPrintResultParameter(const QStandardItemModel *model);
	

protected:
	virtual void run();


private slots:
	void setReplaceFile();
	void setReplaceDir();
	void setReplaceAll();
	void setSkipFile();
	void setSkipDir();
	void setSkipAll();
	void stop();
	void setReply(int reply);
	void parallelWorkCompleted();


private:

	void doSearch();
	void doReplace();
	void doExportToXML();
	void doPrintResult();

	// Retrieval given directory, filter the path need to be search.
	QFileInfoList OperationHandler::retrievalDIr(const QDir &dir, const QString &pattern, const bool isIncludeDir);

	int processSearchReplaceToTemp(
		const QString &filePath, 
		const QString &tempFilePath, 
		QStandardItem *searchResultItem);
	
	void confirmReplaceToOriginal(
		const QString &originalFilePath, 
		const QString &tempFilePath, 
		QStandardItem *searchResultItem);
	
	bool processItemReplace(
		QString &originalContent, 
		QStandardItemModel *replaceListModel, 
		QString &replaceLine, 
		QString &contentLineDisp);

private:
	/* Search and replace member */
	QDir m_searchDir;	// Search or replace directory.
	QString m_strFileFilter;	// Search filter.
	QString m_strSearchPattern;	// Regular searching pattern.
	QString m_strReplacement;	// Content replace to.

	/* Other operation member */
	QString m_strOutPutPath;
	const QStandardItemModel *m_pResultModel;

	bool m_isReplaceFile;
	bool m_isReplaceDir;
	bool m_isReplaceAll;
	bool m_isSkipFile;
	bool m_isSkipDir;
	bool m_isSkipAll;

	bool m_isStop;
	bool m_isCase;
	bool m_isYesToAll;
	bool m_isNoToAll;

	int m_nReplyValue;	// Replay value from user feedback.
	int m_nTotalNeedSearchFile;	// The number of files need to be searched.

	SearcherOption m_option;	// Do search or replace.
	QThreadPool *m_pThreadPool;
	QReadWriteLock m_replyLock;
	MessageDeliver *m_pMessageDeliver;
	QSemaphore *m_pSemaphore;
	QSemaphore *m_pSemaphoreParallel;

};

#endif // SEARCHER_H


